package com.vsd.mobile;


import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.demo.annotation.MobileMethod;
import com.demo.domain.Feedback;
import com.demo.domain.Focus;
import com.demo.mobile.MobException;
import com.demo.mobile.MobileInfo;
import com.demo.service.BFileService;
import com.demo.util.StringUtil;
import com.vsd.domain.City;
import com.vsd.service.CityService;
import com.vsd.service.CodeService;
import com.vsd.service.FeedBackService;
import com.vsd.service.FocusService;
import com.vsd.service.NotifyService;
import com.vsd.service.UserNotifyService;
import com.zy.sys.proto.MOCommon.MCityList;
import com.zy.sys.proto.MOCommon.MFile;
import com.zy.sys.proto.MOCommon.MFileList;
import com.zy.sys.proto.MOCommon.MFocus;
import com.zy.sys.proto.MOCommon.MFocusList;
import com.zy.sys.proto.MOCommon.MRet;
import com.zy.sys.proto.MOCommon.MSysParams;

@Controller
@RequestMapping("/mobile/")
public class MSysController {
	
	@Autowired
	private BFileService fileService;
	@Autowired
	private FeedBackService feedBackService;
	
	@Autowired
	private CodeService codeService;
	
	@Autowired
	private FocusService focusService;
	
	@Autowired
	private NotifyService notifyService;
	
	@Autowired
	private UserNotifyService userNotifyService;
	@Autowired
	private CityService cityService;
	
	
	/**
	 * 文件上传
	 * @param mobileInfo 设备信息
	 * @param requestProto 文件
	 * @return
	 * @throws Exception
	 * url /mSys/mUploadFile?deviceid=
	 */
	@RequestMapping("/mUploadFile")
	@MobileMethod(reqClz = MFileList.class)
	public MRet.Builder mUploadFile(MobileInfo mobileInfo, MFileList.Builder requestProto) throws Exception {
		if (requestProto == null) throw new MobException("files");
		
		String fileId = "";
		
		List<MFile.Builder> list = requestProto.getFileBuilderList();
		
		for (MFile.Builder file : list){
			String temp = fileService.uploadFile(file.getFile().toByteArray(), file.getFileName(), mobileInfo.getUserid());
			if(StringUtils.isNoneBlank(temp)) fileId = fileId + temp + ",";
		}
		
		fileId = fileId.substring(0, fileId.lastIndexOf(","));
		
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(1);
		ret.setMsg(fileId);
		
		return ret;
	}
	
	/**
	 * 用户反馈接口
	 * @param mobileInfo
	 * @param content
	 * @return
	 * @throws Exception
	 * url /mSys/mFeedback?deviceid=&content=
	 */
	@RequestMapping("/mFeedback")
	@MobileMethod
	public MRet.Builder feedback(MobileInfo mobileInfo, String content) throws Exception{
		// 参数校验
		if (StringUtils.isBlank(content)) {
			throw new MobException("content");
		}
		
		Feedback record = new Feedback();
		record.setCreateTime(new Date());
		record.setInfo(content);
		record.setUserId(mobileInfo.getUserid());
		feedBackService.save(record);
		
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(0);
 
		return ret;
	}
	
	
	/**
	 * 系统配置参数
	 * @param mobileInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/mSysParams")
	@MobileMethod
	public MSysParams.Builder sysParams(MobileInfo mobileInfo) throws Exception{
		MSysParams.Builder ret = MSysParams.newBuilder();
		//加载页图片
		ret.setParam1(codeService.getByCode("10001")==null?"":codeService.getByCode("10001").getRemark());
		
//		ret.setParam3(codeService.getCode(ConstantsCode.SHOW_INDEX_RECOMMEND_MODULE));
		return ret;
	}
	
	
	/**
	 * 焦点图列表
	 * @param restraint //地区编号
	 * @param location //焦点图位置  1:首页
	 */
	@RequestMapping("/mFocusList")
	@MobileMethod
	public MFocusList.Builder focusList(MobileInfo mobileInfo, String restraint,Integer location) throws Exception{
		if(location==null) location = 1;
		MFocusList.Builder ret = MFocusList.newBuilder();
		Page<Focus> focus = null;
		Sort sort = new Sort(Direction.DESC, "sort");
		Pageable p = new PageRequest(0, 5,sort);
		if(StringUtils.isNotBlank(restraint)){
			focus = focusService.findByLocationAndRestraint(location, restraint,p);
		}else{
			focus = focusService.findByLocation(location,p);
		}
		for (Focus focu : focus) {
			MFocus.Builder f = MFocus.newBuilder();
			f.setId(focu.getId());
			f.setImg(StringUtil.emptyIfBlank(focu.getImg()));
			f.setRedirectType(focu.getRedirectType());
			f.setRedirectContent(StringUtil.emptyIfBlank(focu.getRedirectContent()));
			if(focu.getRedirectType() == 2){
				f.setRedirectContent("/detail?type=2&id="+focu.getId());
			}
			ret.addList(f);
		}
		return ret;
	}
	
	

	/**
	@api mCityList mCityList	//城市列表
	@param required number location // 1城市列表页 2分类页 3热门 
	@param required string parent //父级编号
	@return MCityList
	*/
	@RequestMapping("/mCityList")
	@MobileMethod
	public MCityList.Builder cityList(MobileInfo mobileInfo,Integer location,String parent) throws Exception{
		if(location==null) location=1;
		MCityList.Builder ret = cityService.selectByLocation(location,parent);
		return ret;
	}
	
	/**
	@api mCityMatch mCityMatch	//城市匹配
	@param required string name //城市名称
	@return MRet  //code 城市编号 name 城市名字
	*/
	@RequestMapping("/mCityMatch")
	@MobileMethod
	public MRet.Builder cityMatch(MobileInfo mobileInfo, String name) throws Exception{
		// 参数校验
		if (StringUtils.isBlank(name)) {
			throw new MobException("name");
		}
		City city = cityService.findByNameAndState(name);
		
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(0);
		ret.setMsg("001,常州");
		if(city!=null){
			ret.setMsg(city.getCode()+","+city.getName());
		}else{
			city = cityService.findDefault();
			ret.setMsg(city.getCode()+","+city.getName());
		}
		return ret;
	}
	
	
}
